package main;
import java.awt.*;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.*;
import creatures.*;


public class Grid{
    /**
     * Contains a 2 by 2 array of cells
     */
    JLabel[][] displayGrid;
    public Cell[][] cellGrid;
    String tile;
    Enemy boss;
	int winType;//determines what win conditions to check
	int floor;//what floor this is
	int startX;
	int startY;
	int [] winX,winY;
	
	boolean inCombat;
	
	//contains list of creatures for multiple enemies
	public ArrayList<Creature> creatures;
    public ArrayList<Occupant> occupants = new ArrayList<Occupant>(); //stores all the occupants in the grid
    //int[] pcCoords = new int[2];
    
    public Grid(int x, int y)
    {
    	if (creatures == null)
    		creatures=new ArrayList<Creature>();
        //note by ben: x = rows, y = columns
        cellGrid=new Cell[x][y]; 
        for (int n = 0; n < x; n++)
        {
            for (int m = 0; m < y; m++)
            {
                cellGrid[n][m] = new Cell(this, "grass");
            }
        }	
    }
    
    //used for kill all grid
    public Grid(int x, int y, int w, int sX, int sY)
    {
    	inCombat = true;
    	winType=w;
    	startX=sX;
    	startY=sY;
    	if (creatures == null)
    		creatures=new ArrayList<Creature>();
        //note by ben: x = rows, y = columns
        cellGrid=new Cell[x][y]; 
        for (int n = 0; n < x; n++)
        {
            for (int m = 0; m < y; m++)
            {
                cellGrid[n][m] = new Cell(this, "grass");
            }
        }	
    }
    
    //used for reach a square grid
    public Grid(int x, int y, int w, int sX, int sY, int [] wX, int [] wY)
    {
    	inCombat = false;
    	winType=w;
    	winX=wX;
    	winY=wY;
    	startX=sX;
    	startY=sY;
    	
    	if (creatures == null)
    		creatures=new ArrayList<Creature>();
        //note by ben: x = rows, y = columns
        cellGrid=new Cell[x][y]; 
        for (int n = 0; n < x; n++)
        {
            for (int m = 0; m < y; m++)
            {
                cellGrid[n][m] = new Cell(this, "grass");
            }
        }	
    }
    
    //used for boss win
    public Grid(int x, int y, int w, int sX, int sY, Enemy b)
    {
    	inCombat = true;
    	winType=w;
    	startX=sX;
    	startY=sY;
    	boss=b;
    	if (creatures == null)
    		creatures=new ArrayList<Creature>();
        //note by ben: x = rows, y = columns
        cellGrid=new Cell[x][y]; 
        for (int n = 0; n < x; n++)
        {
            for (int m = 0; m < y; m++)
            {
                cellGrid[n][m] = new Cell(this, "grass");
            }
        }	
    }
    
    //returns player character
    public Player getPC()
    {
        Iterator<Occupant> itr = occupants.iterator();
        while (itr.hasNext())
        {
            Occupant temp=itr.next();
            if(temp instanceof Player){
                return (Player)temp; 
            }
        }
        return null;
        
    }//make public Player later, IMPORTANT FOR MORE THAN ONE OCCUPANT
    
    //adds the player character in the right position
    public void addPlayer(Player p){
    	p.x=startX;
    	p.y=startY;
    	putOccupant(startX,startY,p);
    }//end method
    
    public void creaturesToGrid(){
        for(Occupant o:creatures){
			putOccupant(o.x, o.y, o);
        }
    }
    
    public double getDToEnemy(Creature enemy){
    	Player p = (Player)getPC();
    	int xPlayer = p.x;
    	int yPlayer = p.y;
    	int xEnemy = enemy.x;
    	int yEnemy = enemy.y;
    	int xDist = xEnemy - xPlayer;
    	int yDist = yEnemy - yPlayer;
    	return (int)Math.sqrt(Math.pow(xDist, 2) + Math.pow(yDist, 2));
    }//finds distance from player to enemy
    
    public int width()
    {
        return cellGrid.length;
    }//x size = ROWS
    
    public int length()
    {
        return cellGrid[0].length;
    }//y size
    
    public void putOccupant(int x, int y, Occupant newOcc)
    {
        newOcc.setHost(x,y,cellGrid[x][y]);
        cellGrid[x][y].addOccupant(newOcc);
        occupants.add(newOcc);
    }//puts an occupant in the specified cell of the coordinates
    
    public void removeOccupant(int x, int y)
    {
        cellGrid[x][y].getOccupant().setHost(0,0,null);
        cellGrid[x][y].removeOccupant();
    }//removes an occupant from a cell
    
    
    
    public void display(Graphics g2)
    {
        Graphics2D g = (Graphics2D)g2;
    }
}